package com.bow.syntax

/**
  * trait相当于Java的接口
  *
  * @author vv
  * @since 2017/7/23.
  */
trait Equal {

  def isEqual(x: Any): Boolean

  /**
    * 可以有方法的实现
    *
    * @param x 入参
    * @return boolean
    */
  def isNotEqual(x: Any): Boolean = !isEqual(x);
}

trait Greater {
  def >(that: Any): Boolean;
}

/**
  * 可以拥有多个trait
  */
class Point(xc: Int, yc: Int) extends Equal with Greater {
  var x: Int = xc
  var y: Int = yc

  def isEqual(that: Any) = that.isInstanceOf[Point] && that.asInstanceOf[Point].x == x

  override def >(that: Any): Boolean = {
    that.isInstanceOf[Point] && {
      val o = that.asInstanceOf[Point];
      this.x + this.y > o.x + o.y;
    }
  }
}


object TraitDemo {

  def main(args: Array[String]) {
    val p1 = new Point(2, 3)
    val p2 = new Point(2, 4)
    val p3 = new Point(3, 3)

    // 以下结果全为true
    println(p1.isEqual(p2))
    println(p1.isNotEqual(p3))
    println(p1.isNotEqual(2)) // not instanceOf
    println(p2 > p1);
  }

}
